package com.zygh.questionnairesurvey.server;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.UserMenu;
import com.zygh.questionnairesurvey.mapper.UserMenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static cn.hutool.core.date.DateUtil.now;

@Service
public class UserMenuService extends ServiceImpl<UserMenuMapper, UserMenu> {

    @Resource
    private UserMenuMapper userMenuMapper;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    public CommonResult listAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserMenu> userMenus = userMenuMapper.selectList(Wrappers.<UserMenu>lambdaQuery()
                .eq(UserMenu::getLevel, 0)
                .orderByAsc(UserMenu::getSort));
        userMenus.forEach(userMenu -> {
            List<UserMenu> userMenus1 = userMenuMapper.selectList(Wrappers.<UserMenu>lambdaQuery()
                    .eq(UserMenu::getLevel, userMenu.getId())
                    .orderByAsc(UserMenu::getSort));
            if (userMenus1.size() > 0) {
                userMenu.setUserMenuList(userMenus1);
            }
        });

        if (userMenus.size() > 0) {
            PageInfo<UserMenu> pageInfo = new PageInfo<>(userMenus);
            return new CommonResult().success(pageInfo);
        }
        return new CommonResult().validateFailed("查询失败！");
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    public CommonResult getById(Integer id) {
        UserMenu userMenus = userMenuMapper.selectById(id);
        if (userMenus != null) {
            return new CommonResult().success(userMenus);
        }
        return new CommonResult().validateFailed("查询失败！");
    }

    /**
     * 新增，插入所有字段
     *
     * @param userMenu 新增的记录
     * @return 返回影响行数
     */
    public CommonResult insert(UserMenu userMenu) {
        userMenu.setCreateDate(now());
        int insert = userMenuMapper.insert(userMenu);
        if (insert > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("新增失败！");
    }


    /**
     * 修改，修改所有字段
     *
     * @param userMenu 修改的记录
     * @return 返回影响行数
     */
    public CommonResult update(UserMenu userMenu) {
        int i = userMenuMapper.updateById(userMenu);
        if (i > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改失败！");
    }


    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    public CommonResult delete(Integer id) {
        int i = userMenuMapper.deleteById(id);
        if (i > 0) {
            userMenuMapper.delete(Wrappers.<UserMenu>lambdaQuery().eq(UserMenu::getLevel, id));
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败！");
    }


    public List<UserMenu> listAlls() {
        List<UserMenu> userMenus = userMenuMapper.selectList(Wrappers.<UserMenu>lambdaQuery()
                .eq(UserMenu::getLevel, 0)
                .orderByAsc(UserMenu::getSort));
        userMenus.forEach(userMenu -> {
            List<UserMenu> userMenus1 = userMenuMapper.selectList(Wrappers.<UserMenu>lambdaQuery()
                    .eq(UserMenu::getLevel, userMenu.getId())
                    .orderByAsc(UserMenu::getSort));
            if (userMenus1.size() > 0) {
                userMenu.setUserMenuList(userMenus1);
            }
        });

        if (userMenus.size() > 0) {
            return userMenus;
        }
        return null;
    }


    public List<UserMenu> findMenuByUser(String username) {
        List<UserMenu> menuByUser = userMenuMapper.findMenuByUser(username, 1, null);
        menuByUser.forEach(userMenu -> {
            List<UserMenu> menuByUser2 = userMenuMapper.findMenuByUser(username, 2, userMenu.getId());
            if (menuByUser2.size() > 0) {
                userMenu.setUserMenuList(menuByUser2.stream().sorted(Comparator.comparingInt(UserMenu::getSort)).collect(Collectors.toList()));
            }
        });

        return menuByUser.stream().sorted(Comparator.comparingInt(UserMenu::getSort)).collect(Collectors.toList());
    }



}